<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>ZDH 模板新增首页</title>
    <meta name="keywords" content="ZDH 模板新增首页">
    <meta name="description" content="ZDH 模板新增首页">

    <link rel="shortcut icon" href="img/favicon.ico">

    <link href="css/plugins/chosen/chosen.css" rel="stylesheet">
    <link href="css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css?v=4.1.0" rel="stylesheet">
    <link href="css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
    <link href="css/plugins/datetimepicker/datetimepicker.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="css/jsplumb/style.css">

    <link href="css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="js/plugins/magicsuggest/magicsuggest.css" rel="stylesheet">
    <style>
        .hidden_li {
            display: none;
        }
    </style>
</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">

    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>模板配置</h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="dropdown-toggle" data-toggle="dropdown" href="buttons.html#">
                            <i class="fa fa-eye"></i>
                        </a>
                        <ul class="dropdown-menu dropdown-user">
                            <li><a href="javascript:void(0);" onclick="getResourceDesc()">功能说明</a>
                            </li>
                        </ul>
                    </div>
                </div>
                <div class="ibox-content">


                    <form id="dispatch_task_add_form" name="dispatch_task_add_form" method="post" class="form-horizontal"
                          action="">
                        <input type="hidden" id="config" name="config" value="">
                        <input type="hidden" id="push_server" name="push_server" value="">

                        <div class="form-group">
                            <label class="col-sm-2 control-label">归属产品</label>
                            <div class="col-sm-10">
                                <select id="product_code" name="product_code"
                                        data-placeholder="归属产品...."
                                        class="chosen-select form-control m-b" >
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">归属组</label>
                            <div class="col-sm-10">
                                <select id="dim_group" name="dim_group"
                                        data-placeholder="归属组...."
                                        class="chosen-select form-control m-b" >
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">模板名称</label>
                            <div class="col-sm-10">
                                <input id="template_name" name="template_name" type="text"
                                       placeholder="模板名称,不可为空" class="form-control" aria-required="true"> </span>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">模板ID</label>
                            <div class="col-sm-10">
                                <input id="template_id" name="template_id" type="text"
                                       placeholder="模板ID,不可为空" class="form-control" aria-required="true"> </span>
                            </div>
                        </div>

                        <div class="form-group" id="div_start_end_time">
                            <label class="col-sm-2 control-label">起始日期</label>
                            <div class="col-sm-10">
                                <input placeholder="开始日期" class="form-control layer-date" id="start_time" name="start_time">
                                <input placeholder="结束日期" class="form-control layer-date" id="end_time" name="end_time">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">消息类型</label>
                            <div class="col-sm-10">
                                <select id="push_type" name="push_type"
                                        data-placeholder="选择消息类型..."
                                        class="chosen-select form-control m-b" tabindex="2">
                                    <option value="" mytype="">选择消息类型...</option>
                                    <option value="1" mytype="">营销</option>
                                    <option value="2" mytype="">通知</option>
                                    <option value="3" mytype="">验证码</option>
                                    <option value="4" mytype="">语音</option>
                                    <option value="5" mytype="">告警</option>
                                    <option value="6" mytype="">其他</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">触达动作</label>
                            <div class="col-sm-10 checkbox i-checks">
                                <label class="no-padding">
                                    <input id="check_sms" name="check_sms"  type="checkbox" ><i></i> 短信</label>
                                <label class="no-padding">
                                    <input id="check_email" name="check_email"  type="checkbox"><i></i> 邮件</label>
                                <label class="no-padding">
                                    <input id="check_push" name="check_push"  type="checkbox"><i></i> APPPUSH</label>
                            </div>
                        </div>



                        <div class="form-group">
                            <label class="col-sm-2 control-label">动作配置</label>
                            <div class="col-sm-10">
                                <div class="tabs-container">
                                    <ul class="nav nav-tabs" id="tab_ul">
                                        <li class="tab_li active" id="check_sms_li"><a data-toggle="tab" href="#check_sms_tab" aria-expanded="true">短信配置</a>
                                        </li>
                                        <li class="tab_li"  id="check_email_li"><a data-toggle="tab" href="#check_email_tab" aria-expanded="false">邮件配置</a>
                                        </li>
                                        <li class="tab_li"  id="check_push_li"><a data-toggle="tab" href="#check_push_tab" aria-expanded="false">APPPUSH配置</a>
                                        </li>
                                    </ul>
                                    <div class="tab-content">
                                        <div id="check_sms_tab" class="tab-pane active">
                                            <div class="panel-body">
                                                <div class="form-group">
                                                    <label class="col-sm-2 control-label">短信通道</label>
                                                    <div class="col-sm-10">
                                                        <select id="channel" name="channel"
                                                                data-placeholder="选择通道...."
                                                                class="chosen-select form-control m-b" >
                                                            <option value="">空</option>
                                                        </select>
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-sm-2 control-label">通道模板</label>
                                                    <div class="col-sm-10">
                                                        <input id="channel_template_id" name="channel_template_id" class="form-control" type="text" value="" placeholder="通道模板ID">
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-sm-2 control-label">短信签名</label>
                                                    <div class="col-sm-10">
                                                        <input id="sign_name" name="sign_name" class="form-control" type="text" value="" placeholder="短信签名">
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-sm-2 control-label">短信内容</label>
                                                    <div class="col-sm-10">
                                                        <textarea id="content" name="content" class="form-control" rows="5" placeholder="短信内容,使用通道模板时,需和通道模板所创建的内容保持一致"></textarea>
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-sm-2 control-label">短信参数</label>
                                                    <div class="col-sm-8">
                                                        <input id="params" name="params" class="form-control" placeholder="短信参数">
                                                    </div>
                                                    <div class="col-sm-2">
                                                        <input id="search" οnsubmit='return false' type="button" class="btn btn-primary form-control" style="margin-bottom:0px" value="查询"/>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>

                                        <div id="check_email_tab" class="tab-pane active">
                                            <div class="panel-body">
                                                <div class="form-group">
                                                    <label class="col-sm-2 control-label">邮箱通道</label>
                                                    <div class="col-sm-10">
                                                        <select id="email_channel" name="email_channel"
                                                                data-placeholder="选择通道...."
                                                                class="chosen-select form-control m-b" >
                                                            <option value="">空</option>
                                                        </select>
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-sm-2 control-label">主题</label>
                                                    <div class="col-sm-10">
                                                        <input id="title" name="title" class="form-control" type="text" value="" placeholder="主题">
                                                    </div>

                                                </div>

                                                <div class="form-group">
                                                    <label class="col-sm-2 control-label">邮件内容</label>
                                                    <div class="col-sm-10">
                                                        <textarea id="email_content" class="form-control" rows="5" placeholder="短信内容,使用通道模板时,需和通道模板所创建的内容保持一致"></textarea>
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-sm-2 control-label">邮件参数</label>
                                                    <div class="col-sm-8">
                                                        <input id="email_params" class="form-control" placeholder="邮件参数">
                                                    </div>
                                                    <div class="col-sm-2">
                                                        <input id="search1" οnsubmit='return false' type="button" class="btn btn-primary form-control" style="margin-bottom:0px" value="查询"/>
                                                    </div>
                                                </div>

                                            </div>
                                        </div>

                                        <div id="check_push_tab" class="tab-pane active">
                                            <div class="panel-body">
                                                <div class="form-group">
                                                    <label class="col-sm-2 control-label">APP</label>
                                                    <div class="col-sm-10">
                                                        <select id="app" name="app"
                                                                data-placeholder="选择APP...."
                                                                class="chosen-select form-control m-b" >
                                                            <option value="">空</option>
                                                        </select>
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-sm-2 control-label">消息应用场景</label>
                                                    <div class="col-sm-10">
                                                        <select id="scene" name="scene"
                                                                data-placeholder="选择消息应用场景...."
                                                                class="chosen-select form-control m-b" >
                                                            <option value="0">空</option>
                                                            <option value="1">资讯营销</option>
                                                            <option value="2">内容推广</option>
                                                            <option value="3">服务通知</option>
                                                            <option value="4">提醒告警</option>
                                                        </select>
                                                    </div>
                                                </div>

                                                <div class="form-group">
                                                    <label class="col-sm-2 control-label">消息点击类型</label>
                                                    <div class="col-sm-10">
                                                        <select id="click_type" name="click_type"
                                                                data-placeholder="选择消息类型...."
                                                                class="chosen-select form-control m-b" >
                                                            <option value="0">空</option>
                                                            <option value="1">跳转app内部页面</option>
                                                            <option value="2">跳转app</option>
                                                            <option value="3">跳转外部链接</option>
                                                        </select>
                                                    </div>
                                                </div>

                                                <div class="form-group">
                                                    <label class="col-sm-2 control-label">APP页面</label>
                                                    <div class="col-sm-10">
                                                        <select id="click_app" name="click_app"
                                                                data-placeholder="选择APP页面...."
                                                                class="chosen-select form-control m-b" >
                                                            <option value="0">空</option>
                                                        </select>
                                                    </div>
                                                </div>

                                                <div class="form-group">
                                                    <label class="col-sm-2 control-label">跳转外部资源链接</label>
                                                    <div class="col-sm-10">
                                                        <input id="click_url" name="click_url" class="form-control" type="text" value="" placeholder="跳转外部资源链接">
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-sm-2 control-label">标题</label>
                                                    <div class="col-sm-10">
                                                        <textarea id="push_title" name="title" class="form-control" rows="1" placeholder="标题"></textarea>
                                                    </div>
                                                </div>

                                                <div class="form-group">
                                                    <label class="col-sm-2 control-label">内容</label>
                                                    <div class="col-sm-10">
                                                        <textarea id="push_content" name="content" class="form-control" rows="5" placeholder="内容"></textarea>
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-sm-2 control-label">PUSH参数</label>
                                                    <div class="col-sm-8">
                                                        <input id="push_params" name="params" class="form-control" placeholder="PUSH参数">
                                                    </div>
                                                    <div class="col-sm-2">
                                                        <input id="push_search" οnsubmit='return false' type="button" class="btn btn-primary form-control" style="margin-bottom:0px" value="查询"/>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">版本说明</label>
                            <div class="col-sm-10">
                                <input id="version_context" name="version_context" type="text"
                                       placeholder="版本说明,用户手动保存版本,可根据历史版本还原当前配置" class="form-control" aria-required="true"> </span>
                            </div>
                        </div>

                        <div class="hr-line-dashed"></div>


                        <div class="form-group">
                            <div class="text-center">
                                <button id="save_dispatch_task" name="save_zdh" class="btn btn-primary" οnsubmit='return false'
                                        type="button">保存
                                </button>
                                <button id="update_dispatch_task" name="save_zdh" class="btn btn-primary" οnsubmit='return false'
                                        type="button">更新
                                </button>
                                <button id="save_log_dispatch_task" name="save_log_zdh" class="btn btn-warning" οnsubmit='return false'
                                        type="button">保存版本
                                </button>
                                <button id="reset" class="btn btn-white" type=reset>清空</button>
                            </div>
                        </div>
                    </form>

                </div>
            </div>



        </div>
    </div>
</div>


<script src="js/zdh_common.js"></script>
<!-- 全局js -->
<script src="js/jquery.min.js?v=2.1.4"></script>
<script src="js/jquery-ui.custom.min.js?v=2.1.4"></script>
<script src="js/jquery-ui-1.10.4.min.js"></script>
<script src="js/bootstrap.min.js?v=3.3.6"></script>

<!--<script type="text/javascript" src="js/plugins/suggest/bootstrap-suggest.min.js"></script>-->
<script type="text/javascript" src="js/plugins/magicsuggest/magicsuggest.js"></script>

<!-- 自定义js -->
<script src="js/plugins/metisMenu/jquery.metisMenu.js"></script>
<script src="js/plugins/slimscroll/jquery.slimscroll.min.js"></script>
<script src="js/hplus.js?v=4.1.0"></script>
<script src="js/content.js?v=1.0.0"></script>

<!-- Chosen -->
<script src="js/plugins/chosen/chosen.jquery.js"></script>

<!-- layer javascript -->
<script src="js/plugins/layer/layer.min.js"></script>

<!-- Toastr script -->
<script src="js/plugins/toastr/toastr.min.js"></script>

<!-- iCheck -->
<script src="js/plugins/iCheck/icheck.min.js"></script>

<script src="js/plugins/datetimepicker/bootstrap-datetimepicker.min.js"></script>

<script src="js/plugins/datetimepicker/bootstrap-datetimepicker.zh-CN.js"></script>

<script src="js/plugins/layer/laydate/laydate.js"></script>

<script src="js/admin/dim_product_common.js"></script>
<script src="js/admin/dim_group_common.js"></script>

<script type="text/javascript" src="js/jsplumb/jquery.jsPlumb.min.js"></script>
<script type="text/javascript" src="js/push/push_template_add.js"></script>

<script>
    $(document).ready(function () {
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green',
        });

        $('input[type=checkbox]').on('ifChanged', function(){
            var id_str = $(this).attr('id');
            if($(this).is(":checked")){
                // $('.tab-pane').removeClass("active");
                // $('.tab_li').removeClass("active");
                // $('#'+id_str+"_tab").addClass("active");
                // $('#'+id_str+"_li").addClass("active");
                $('#'+id_str+"_li").click();
            }else{
                // $('#'+id_str+"_tab").removeClass("active");
                // $('#'+id_str+"_li").removeClass("active");
                //
                // //遍历所有check
                // var checkboxes = $('input[type="checkbox"]');
                //
                // checkboxes.each(function(index, checkbox) {
                //     // 在这里执行对每个checkbox的操作
                //     var check_id = $(checkbox).attr("id");
                //     if($(checkbox).is(":checked")){
                //         $('.tab-pane').removeClass("active");
                //         $('#'+check_id+"_tab").addClass("active");
                //         $('#'+check_id+"_li").addClass("active");
                //         return false;
                //     }
                // });

            }

        })
    });

    (function(document, window, $) {


        (function () {

            var url = location.search; //这一条语句获取了包括问号开始到参数的最后，不包括前面的路径
            var params = url.substr(1);//去掉问号
            var pa = params.split("&");
            var s = new Object();
            for(var i = 0; i < pa.length; i ++){
                s[pa[i].split("=")[0]] = unescape(pa[i].split("=")[1]);
            }

            //$('.tab-pane').removeClass("active");
            //init_job_sources();
            //s.id=-1 表示新增,否则表示更新
            if(s.id==-1){
                $('#update_dispatch_task').hide();
                $('#save_dispatch_task').show();
            }else{
                if(s.is_copy == "true"){
                    console.info("拷贝按钮触发");
                    $('#update_dispatch_task').hide();
                    $('#save_dispatch_task').show();
                }else{
                    console.info("更新按钮触发");
                    $('#save_dispatch_task').hide();
                    $('#update_dispatch_task').show();
                }

            }
            // ------------------------------------------------------------------
            //日期范围限制
            var start = {
                elem: '#start_time',
                format: 'YYYY-MM-DD hh:mm:ss',
                min: '2000-01-01 00:00:00',
                max: '2099-06-16 23:59:59', //最大日期
                istime: true,
                istoday: false,
                choose: function (datas) {
                    end.min = datas; //开始日选好后，重置结束日的最小日期
                    end.start = datas //将结束日的初始值设定为开始日
                }
            };
            var end = {
                elem: '#end_time',
                format: 'YYYY-MM-DD hh:mm:ss',
                min: '2000-01-01 00:00:00',
                max: '2099-06-16 23:59:59',
                istime: true,
                istoday: false,
                choose: function (datas) {
                    start.max = datas; //结束日选好后，重置开始日的最大日期
                }
            };
            laydate(start);
            laydate(end);

            $("#start_time").val(getCurrentDate());
            $("#end_time").val(getCurrentDate());

            function getCurrentDate(){
                var oDate = new Date(),
                    oYear = oDate.getFullYear(),
                    oMonth = oDate.getMonth()+1,
                    oDay = oDate.getDate(),
                    oHour = oDate.getHours(),
                    oMin = oDate.getMinutes(),
                    oSen = oDate.getSeconds(),
                    oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay)+" "+getzf(oHour)+":"+getzf(oMin)+":"+getzf(oSen);//最后拼接时间
                return oTime;
            };

            function getMyDate(str){
                var oDate = new Date(str),
                    oYear = oDate.getFullYear(),
                    oMonth = oDate.getMonth()+1,
                    oDay = oDate.getDate(),
                    oHour = oDate.getHours(),
                    oMin = oDate.getMinutes(),
                    oSen = oDate.getSeconds(),
                    oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay)+" "+getzf(oHour)+":"+getzf(oMin)+":"+getzf(oSen);//最后拼接时间
                return oTime;
            };
            //补0操作
            function getzf(num){
                if(parseInt(num) < 10){
                    num = '0'+num;
                }
                return num;
            }

            //$('#channel').chosen();
            //初始化通道code
            function init_push_channel(id, channel_type){
                $.ajax({
                    type: 'POST',
                    url: server_context+"/push_channel_list",
                    async:false,
                    dataType: 'json',
                    data: {"channel_type": channel_type},
                    //成功返回
                    success: function (data) {
                        if(data.code != "200"){
                            layer.msg(data.msg);
                            return ;
                        }
                        var str = '<option value=\"'  + '\" hassubinfo=\"true\">' + '选择通道' + '</option>';
                        for (var i = 0; i < data.result.length; i++) {
                            str += '<option value=\"' + data.result[i].channel_code + '\" hassubinfo=\"true\">' + data.result[i].channel_name + '</option>';
                        }
                        $('#'+id).html(str);
                        $('#'+id).trigger("chosen:updated");
                        $('#'+id).chosen();
                    },
                    //处理完成
                    complete: function () {
                    },
                    //报错
                    error: function (data) {
                    }
                });
            }

            function init_push_app(id){
                $.ajax({
                    type: 'POST',
                    url: server_context+"/push_app_list",
                    async:false,
                    dataType: 'json',
                    data: {},
                    //成功返回
                    success: function (data) {
                        if(data.code != "200"){
                            layer.msg(data.msg);
                            return ;
                        }
                        var str = '<option value=\"'  + '\" hassubinfo=\"true\">' + '选择App' + '</option>';
                        for (var i = 0; i < data.result.length; i++) {
                            str += '<option value=\"' + data.result[i].app + '\" hassubinfo=\"true\">' + data.result[i].app_name + '</option>';
                        }
                        $('#'+id).html(str);
                        $('#'+id).trigger("chosen:updated");
                        $('#'+id).chosen();
                    },
                    //处理完成
                    complete: function () {
                    },
                    //报错
                    error: function (data) {
                    }
                });
            }

            init_push_channel('channel', "sms");


            //此处增加样式为了解决chosen插件宽度为0问题
            $('#check_email_tab').addClass("active");
            init_push_channel('email_channel', "email");
            $('#check_email_tab').removeClass("active");

            $('#check_push_tab').addClass("active");
            init_push_app('app');
            $('#check_push_tab').removeClass("active");

            if(s.id !='-1'){
                console.info("更新-拷贝按钮触发");
                //开始赋值
                $.ajax({
                    url : server_context+"/push_template_detail",
                    data : "id=" + s.id,
                    type : "post",
                    dataType : "json",
                    success : function(data) {
                        if(data.code != "200"){
                            layer.msg(data.msg);
                            return ;
                        }
                        console.info("success");
                       // layer.alert(JSON.stringify(data[0]))
                        //获取调度任务说明
                        var template_name=data.result.template_name;
                        $("#template_name").val(template_name);

                        var product_code=data.result.product_code;
                        $("#product_code").val(product_code);
                        $("#product_code").trigger("chosen:updated");

                        var dim_group=data.result.dim_group;
                        $("#dim_group").val(dim_group);
                        $("#dim_group").trigger("chosen:updated");

                        var template_id=data.result.template_id;
                        $("#template_id").val(template_id);

                        var push_type=data.result.push_type;
                        $("#push_type").val(push_type);
                        $("#push_type").trigger("chosen:updated");

                        var push_servers=data.result.push_server.split(",");
                        for (var i = 0; i < push_servers.length; i++) {
                            if(!is_empty(push_servers[i])){
                                $('#check_'+push_servers[i]).iCheck('check');
                            }
                        }

                        //sms配置
                        if(!is_empty(data.result.configMap.sms)){
                            $("#channel").val(data.result.configMap.sms.channel);
                            $("#channel").trigger("chosen:updated");

                            $("#channel_template_id").val(data.result.configMap.sms.channel_template_id);
                            $("#sign_name").val(data.result.configMap.sms.sign_name);
                            $("#content").val(data.result.configMap.sms.content);
                            $("#params").val(data.result.configMap.sms.params);
                        }

                        if(!is_empty(data.result.configMap.email)){
                            $("#email_channel").val(data.result.configMap.email.channel);
                            $("#email_channel").trigger("chosen:updated");

                            $("#title").val(data.result.configMap.email.title);
                            $("#email_content").val(data.result.configMap.email.content);
                            $("#email_params").val(data.result.configMap.email.params);

                        }

                        if(!is_empty(data.result.configMap.push)){
                            $("#app").val(data.result.configMap.push.app);
                            $("#app").trigger("chosen:updated");

                            $("#scene").val(data.result.configMap.push.scene);
                            $("#scene").trigger("chosen:updated");

                            $("#click_type").val(data.result.configMap.push.click_type);
                            $("#click_type").trigger("chosen:updated");

                            $("#click_app").val(data.result.configMap.push.click_app);
                            $("#click_app").trigger("chosen:updated");

                            $("#click_url").val(data.result.configMap.push.click_url);


                            $("#push_title").val(data.result.configMap.push.title);
                            $("#push_content").val(data.result.configMap.push.content);
                            $("#push_params").val(data.result.configMap.push.params);

                        }

                        //开始时间
                        var start_time=data.result.start_time;
                        if(start_time!='' && typeof(start_time) !="undefined"){
                            $("#start_time").val(getMyDate(start_time));
                        }

                        //结束时间
                        var end_time=data.result.end_time;
                        if(end_time!='' && typeof(end_time) !="undefined"){
                            $("#end_time").val(getMyDate(end_time));
                        }


                    },
                    error: function (data) {
                        console.info("error: " + data.responseText);
                    }

                });
            }


        })();



    })(document, window, jQuery);



    function buildConfig(){
        var sms_config = {
            "channel": $("#channel").val(),
            "channel_template_id": $("#channel_template_id").val(),
            "sign_name": $("#sign_name").val(),
            "content": $("#content").val(),
            "params": $("#params").val()
        };

        var email_config = {
            "channel": $("#email_channel").val(),
            "title": $("#title").val(),
            "content": $("#email_content").val(),
            "params": $("#email_params").val()
        };

        var push_config = {
            "app": $("#app").val(),
            "scene": $("#scene").val(),
            "click_type": $("#click_type").val(),
            "click_app": $("#click_app").val(),
            "click_url": $("#click_url").val(),
            "title": $("#push_title").val(),
            "content": $("#push_content").val(),
            "params": $("#push_params").val()
        };


        var config = {
            "sms": sms_config,
            "email": email_config,
            "push": push_config
        };

        return config;
    }

    function buildPushServer(){
        var push_server = "";
        if($('#check_sms').is(":checked")){
            push_server = "sms";
        }
        if($('#check_email').is(":checked")){
            if(is_empty(push_server)){
                push_server = "email"
            }else{
                push_server = push_server +",email"
            }
        }
        if($('#check_push').is(":checked")){
            if(is_empty(push_server)){
                push_server = "push"
            }else{
                push_server = push_server +",push"
            }
        }
        return push_server;
    }

    $('#save_dispatch_task').click(function () {




        var config = buildConfig();
        var push_server = buildPushServer();
        $('#config').val(JSON.stringify(config));
        $('#push_server').val(push_server);

        $.ajax({
            type: 'POST',
            url: server_context+"/push_template_add",
            dataType: 'json',
            data: $("#dispatch_task_add_form").serialize(),
            //发送数据前
            beforeSend: function () {
                // 禁用按钮防止重复提交
                $("#save_dispatch_task").attr({disabled: "disabled"});
            },
            //成功返回
            success: function (data) {
                if(data.code != "200"){
                    layer.msg(data.msg);
                    return
                }
                $("#save_dispatch_task").removeAttr('disabled');
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
                //closeTab();
            },
            //处理完成
            complete: function () {
                $("#save_dispatch_task").removeAttr('disabled');
                console.info("complete")
            },
            //报错
            error: function (data) {
                $("#save_dispatch_task").removeAttr('disabled');
                layer.msg(data.responseText);
                console.info("error: " + data.responseText);
            }
        });


    });

    $('#update_dispatch_task').click(function () {

        var url = location.search; //这一条语句获取了包括问号开始到参数的最后，不包括前面的路径
        var params = url.substr(1);//去掉问号
        var pa = params.split("&");
        var s = new Object();
        for(var i = 0; i < pa.length; i ++){
            s[pa[i].split("=")[0]] = unescape(pa[i].split("=")[1]);
        }


        var config = buildConfig();
        var push_server = buildPushServer();
        $('#config').val(JSON.stringify(config));
        $('#push_server').val(push_server);


        $.ajax({
            type: 'POST',
            url: server_context+"/push_template_update",
            dataType: 'json',
            data: $("#dispatch_task_add_form").serialize()+'&id='+s.id,
            //发送数据前
            beforeSend: function () {
                // 禁用按钮防止重复提交
                $("#update_dispatch_task").attr({disabled: "disabled"});
            },
            //成功返回
            success: function (data) {
                if(data.code != '200'){
                    parent.layer.msg("更新失败"+data.msg);
                    return ;
                }
                $("#update_dispatch_task").removeAttr('disabled');
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
               // closeTab()
            },
            //处理完成
            complete: function () {
                $("#update_dispatch_task").removeAttr('disabled');
                console.info("complete")
            },
            //报错
            error: function (data) {
                $("#update_dispatch_task").removeAttr('disabled');
                layer.msg(data.responseText);
                console.info("error: " + data.responseText);
            }
        });

    });

</script>


</body>

</html>
